//
//  CalculationForTouristVC.m
//  SelfDrivingTravelling-IOS
//
//  Created by Teplot_01 on 16/10/31.
//  Copyright © 2016年 Teplot_01. All rights reserved.
//

#import "CalculationForTouristVC.h"
#import "ActivityListVC.h"
#import "MyBillDetailVC.h"
@interface CalculationForTouristVC ()<UITableViewDelegate,UITableViewDataSource>


@end

@implementation CalculationForTouristVC
{
    UITableView *_tableView;
    
    //请求下来的字典
    NSDictionary *_responseDic;
    //请求下来的数组
    NSArray *_responseArr;
    NSArray *_resArr;
    
    NSString *_activityIdStr;
}
- (void)viewWillAppear:(BOOL)animated{
    [self requestForBillList];
}
- (void)viewDidLoad {
    [super viewDidLoad];
     self.view.backgroundColor = superBackgroundColor;
    [self initTableView];
    _tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
}

- (void)requestForBillList{
    //参数
    NSDictionary *parameters = @{
                                  @"userID": [NSString readToNSDefaultsWithKey:kId],
                                  @"status":@"2",
                                 };
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager POST:@"http://115.28.152.130/tour/v1/activity/userBillList.action" parameters:parameters success:^(NSURLSessionDataTask *operation, id responseObject) {
        _responseDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"%@><><><><>>>",_responseDic);
        _responseArr = _responseDic[@"list"];
        if ([_responseDic[@"code"] isEqualToString:@"3003"]) {
            [self initRemindView];
        }else{
            //请求成功
//            [SVProgressHUD showSuccessWithStatus:@"账单列表请求成功"];
            [self initTopView];
            [_tableView reloadData];
        }
        
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"请求失败"];
    }];
}
- (void)initTopView{
    UIView *topView = [[UIView alloc]initWithFrame:CGRectMake(0, 64, KScreenWidth, 45)];
    topView.backgroundColor = superBackgroundColor;
    [self.view addSubview:topView];
    UILabel *totalLb = [[UILabel alloc]initWithFrame:CGRectMake(30, 10, KScreenWidth-60, 35)];
    NSNumber *num = _responseArr[0][@"activityTotalMoney"];
    
    totalLb.text = [NSString stringWithFormat:@"%@%@%@",@"此次活动截止目前共消费:",num,@"元"];
    totalLb.backgroundColor = Theme_Color;
    totalLb.textColor = White_Color;
    totalLb.font = [UIFont systemFontOfSize:14];
    totalLb.textAlignment = NSTextAlignmentCenter;
    //阴影效果
    totalLb.layer.shadowColor = [UIColor grayColor].CGColor;
    totalLb.layer.shadowOffset = CGSizeMake(2,2);//shadowOffset阴影偏移,x向右偏移4，y向下偏移4，默认(0, -3),这个跟shadowRadius配合使用
    totalLb.layer.shadowOpacity = 0.5;
    totalLb.layer.shadowRadius = 2;
    [topView addSubview:totalLb];
}
- (void)initTableView{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(15, 45, KScreenWidth - 30, KScreenHeight -63+20)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.backgroundColor = superBackgroundColor;
    [self.view addSubview:_tableView];
}
- (void)initRemindView{
    UILabel *lb = [[UILabel alloc]initWithFrame:CGRectMake(20, 180, KScreenWidth-40, 60)];
    lb.numberOfLines = 2;
    lb.textAlignment = NSTextAlignmentCenter;
    lb.textColor = Gray_Color;
    lb.font = [UIFont systemFontOfSize:15];
    lb.text = @"啊哦～没有正在进行的活动哦～快来选择感兴趣的活动加入我们吧!";
    [self.view addSubview:lb];
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(KScreenWidth/2-60, 300, 120, 30)];
    [btn setTitle:@"查看热门活动" forState:UIControlStateNormal];
    btn.layer.borderWidth = 1;
    btn.layer.borderColor = Theme_Color.CGColor;
    btn.layer.cornerRadius = 5;
    [btn setTitleColor:Theme_Color forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:13];
    [btn addTarget:self action:@selector(clickAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}
#pragma - mark UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    _resArr = _responseArr[section][@"billList"];
    return _resArr.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 35;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc]init];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    UIImageView *imageV = [[UIImageView alloc]initWithFrame:CGRectMake(18, 7, 20, 20)];
    imageV.image = [UIImage imageNamed:@"sun"];
    [cell.contentView addSubview:imageV];
    UILabel *dayLb = [[UILabel alloc]initWithFrame:CGRectMake(40, 0, 100, 35)];
    dayLb.text = _resArr[indexPath.row][@"payTime"];
    dayLb.font = [UIFont systemFontOfSize:14];
    [cell.contentView addSubview:dayLb];
    UILabel *daySpendLb = [[UILabel alloc]initWithFrame:CGRectMake(KScreenWidth-150, 0, 100, 35)];
    NSString *dayTotal = [NSString stringWithFormat:@"%@%@", _resArr[indexPath.row][@"money"],@"元"];
    daySpendLb.text = dayTotal;
    daySpendLb.font = [UIFont systemFontOfSize:14];
    daySpendLb.textAlignment = NSTextAlignmentRight;
    [cell.contentView addSubview:daySpendLb];
    UIImageView *triangleImaV = [[UIImageView alloc]initWithFrame:CGRectMake(KScreenWidth-45, 12, 5, 10)];
    triangleImaV.image = [UIImage imageNamed:@"nextnew"];
    [cell.contentView addSubview:triangleImaV];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(10, 0, KScreenWidth, 30)];
    if (_responseArr.count == 0) {
        return customView;
    }else{
        customView.backgroundColor = superBackgroundColor;
        UILabel *nameLb = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, 200, 30)];
        NSString *nameStr = [NSString stringWithFormat:@"%@%@",@"活动名:  ",_responseArr[0][@"activityName"]];
        nameLb.text = nameStr;
        nameLb.textAlignment = NSTextAlignmentLeft;
        nameLb.font = [UIFont systemFontOfSize:15];
        [customView addSubview:nameLb];
        return customView;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MyBillDetailVC *vc = [[MyBillDetailVC alloc]init];
    vc.dayMoney = _resArr[indexPath.row][@"money"];
    vc.time = _resArr[indexPath.row][@"payTime"];
    vc.activityName = _responseArr[0][@"activityName"];
    vc.dayDetailArr = _resArr[indexPath.row][@"billList"];
    [self.navigationController pushViewController:vc animated:YES];
    
}
- (void)clickAction{
    ActivityListVC *vc = [[ActivityListVC alloc]init];
    vc.hidesBottomBarWhenPushed = YES;
    vc.status = @"1";
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
